<template>
  <div class='member-order'>
    <!-- tab组件 -->
    <XtxTabs v-model="activeName" @tab-click="tabClick">
      <XtxTabsPanel v-for="item in orderStatus" :key="item" :label="item.label" :name="item.name"></XtxTabsPanel>
    </XtxTabs>
    <!-- 订单列表 -->
    <div class="order-list">
      <!-- loading加载 -->
      <div v-if="loading" class="loading"></div>
      <div class="none" v-if="!loading && orderList.length === 0">暂无数据</div>
      <OrderItem @on-logistics="handlerLogistics" @on-cancel="handlerCancel" @on-delete="handlerDelete"
        @on-confirm="handlerConfirm" v-for="item in orderList" :key="item.id" :order='item'>
      </OrderItem>
    </div>
    <!-- 分页组件 -->
    <XtxPagination @current-page="reqParams.page = $event" v-if="total > 0" :current-page="reqParams.page"
      :page-size="reqParams.pageSize" :total="total">
    </XtxPagination>
    <!-- 取消原因对话框 -->
    <OrderCancel ref="orderCancelCom"></OrderCancel>
    <!-- 查看物流组件 -->
    <OrderLogistics ref="orderLogistics"></OrderLogistics>
  </div>
</template>

<script>
import { reactive, ref, watch } from 'vue'
import OrderItem from './components/order-item.vue'
import { confirmOrder, deleteOrder, findOrderList } from '@/api/order'
import { orderStatus } from '@/api/constants'
import OrderCancel from './components/order-cancel.vue'
import Confirm from '@/components/library/Confirm'
import Message from '@/components/library/Message'
import OrderLogistics from './components/order-logistics.vue'
export default {
  name: 'MemberOrder',
  components: {
    OrderItem,
    OrderCancel,
    OrderLogistics
  },
  setup() {
    const activeName = ref('all')
    const loading = ref(false) //控制tab内容的loading样式
    const total = ref(0) //分页总条数

    //获取订单信息
    //查询参数
    const reqParams = reactive({
      page: 1,
      pageSize: 5,
      orderState: 0
    })
    const orderList = ref([])

    //监听tab的切换重新发起请求加载页面数据

    const getOrderList = () => {
      loading.value = true
      findOrderList(reqParams).then(data => {
        orderList.value = data.result.items
        loading.value = false
        total.value = data.result.counts
      })
    }

    watch(reqParams, () => {
      getOrderList()
    }, { immediate: true })



    //点击tab切换按钮
    const tabClick = ({ index }) => {
      reqParams.page = 1,
        reqParams.orderState = index
    }

    //删除订单
    const handlerDelete = (order) => {
      Confirm({ text: '亲，您确定要删除该订单吗' }).then(() => {
        deleteOrder(order.id).then(() => {
          Message({ type: 'success', text: '删除订单成功' })
          getOrderList()
        })
      }).catch(() => { })
    }

    return {
      activeName,
      tabClick,
      orderList,
      orderStatus,
      loading,
      total,
      reqParams,
      ...useCancel(),
      handlerDelete,
      ...uesConfirm(),
      ...useLogistics()
    }
  }

}
//取消订单逻辑
export const useCancel = () => {
  //组件实例
  const orderCancelCom = ref(null)

  const handlerCancel = (order) => {
    orderCancelCom.value.open(order)
  }

  return {
    handlerCancel,
    orderCancelCom
  }
}

//确认收货逻辑
export const uesConfirm = () => {
  const handlerConfirm = (order) => {
    Confirm({ text: '亲，您确定已经收到货物了吗' }).then(() => {
      confirmOrder(order.id).then(() => {
        Message({ type: 'success', text: '确认收货成功' })
        //待收货---待评价
        order.orderState = 4
      })
    }).catch(() => { })
  }
  return {
    handlerConfirm
  }
}

//查看物流逻辑
const useLogistics = () => {
  const orderLogistics = ref(null)
  const handlerLogistics = (order) => {
    orderLogistics.value.open(order)
  }
  return {
    handlerLogistics,
    orderLogistics
  }
}
</script>

<style lang='less' scoped>
.order-list {
  background: #fff;
  padding: 20px;
  position: relative;
}

.loading {
  height: 100%;
  width: 100%;
  position: absolute;
  left: 0;
  top: 0;
  background: rgba(255, 255, 255, .9) url(../../../assets/image/loading.gif) no-repeat center;
}

.none {
  height: 400px;
  text-align: center;
  line-height: 400px;
  color: #999;
}
</style>